<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>35translate实现水平垂直居中</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .box {
            position: relative;
            width: 400px;
            height: 400px;
            background-color: pink;
        }
        
        p {
            position: absolute;
            top: 50%;
            left: 50%;
            /* margin-top: -100px; */
            /* margin-left: -100px; */
            transform: translate(-50%, -50%);
            width: 200px;
            height: 200px;
            background-color: purple;
            color: #fff;
        }
    </style>
</head>

<body>
    <div class="box">
        <p>transform: translate(-50%, -50%);</p>
    </div>
</body>

</html>